﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Reflection;

namespace PDAMain.DAL
{
    class ConfigHelper
    {
        public static string ConfigPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString()) + @"\config.xml";

        /// <summary>
        /// 获取配置文件中的信息
        /// </summary>
        /// <returns></returns>
        public static string GetInfo()
        {
            string Webservice = string.Empty;
            string Time = string.Empty;
            string Mac = string.Empty;
            string Picurl = string.Empty;
            string Port = string.Empty;
            string User = string.Empty;
            string Pwd = string.Empty;
            XmlDocument xml = new XmlDocument();
            xml.Load(ConfigPath);
            XmlNode xn = xml.SelectSingleNode("Settings");
            XmlNodeList xnl = xn.ChildNodes;
            foreach (XmlNode xnf in xnl)
            {
                if (xnf.Name == "Webservice")
                {
                    Webservice = xnf.InnerText;
                }
                else if (xnf.Name == "Time")
                {
                    Time = xnf.InnerText;
                }
                else if (xnf.Name == "Mac")
                {
                    Mac = xnf.InnerText;
                }
                else if (xnf.Name == "PicUrl")
                {
                    Picurl = xnf.InnerText;
                }
                else if (xnf.Name == "Port")
                {
                    Port = xnf.InnerText;
                }
                else if (xnf.Name == "User")
                {
                    User = xnf.InnerText;
                }
                else if (xnf.Name == "Pwd")
                {
                    Pwd = xnf.InnerText;
                }
            }
            return Webservice + "," + Time + "," + Mac + "," + Picurl + "," + Port + "," + User + "," + Pwd;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConfigValue(string key)
        {
            string Webservice = string.Empty;
            string Time = string.Empty;
            string Mac = string.Empty;
            XmlDocument xml = new XmlDocument();
            xml.Load(ConfigPath);
            XmlNode xn = xml.SelectSingleNode("Settings");
            XmlNodeList xnl = xn.ChildNodes;
            foreach (XmlNode xnf in xnl)
            {
                if (xnf.Name == key)
                {
                    return xnf.InnerText;
                }
            }
            return "";
        }

        public static bool CheckConfigFile()
        {
            return File.Exists(ConfigPath);
        }

        /// <summary>
        /// 自动创建配置文件
        /// </summary>
        /// <returns></returns>
        public static bool InitConfigFile()
        {
            bool result = true;
            try
            {
                if (!File.Exists(ConfigPath))
                {
                    string info = "<Settings><Webservice></Webservice><Time>300000</Time><Mac>RNDISFN1</Mac><PicUrl></PicUrl><Port></Port><User></User><Pwd></Pwd></Settings>";
                    XmlDocument xml = new XmlDocument();
                    xml.LoadXml(info);
                    xml.Save(ConfigPath); ;
                }
            }
            catch (Exception ex)
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 获取配置文件中关于数据同步的时间间隔
        /// </summary>
        /// <returns></returns>
        public static int GetTime()
        {
            int result = 300000;//默认为5分钟同步一次
            XmlDocument xml = new XmlDocument();
            xml.Load(ConfigPath);
            XmlNode xn = xml.SelectSingleNode("Settings");
            XmlNodeList xnl = xn.ChildNodes;
            foreach (XmlNode xnf in xnl)
            {
                if (xnf.Name == "Time")
                {
                    result = Convert.ToInt32(xnf.InnerText);
                    break;
                }
            }
            return result;
        }
    }
}
